package com.cumt.threeway.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.cumt.threeway.R;
import com.cumt.threeway.util.ThreeWayUtil;
import com.cumt.threeway.view.dialog.CheckCodeDialog;

/**
 * Created by 胡晶晶 on 2016/5/6.
 */
public class RegisterActivity extends FragmentActivity implements View.OnClickListener {

    private EditText mAccountView;
    private EditText mPasswordView;
    private EditText mNickNameView;
    private View mDelAccountView;
    private View mDelPasswordView;
    private View mDelNickNameView;
    private RequestQueue queue;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_layout);
        initView();
    }

    private void initView() {
        queue = Volley.newRequestQueue(this);
        mAccountView = (EditText) findViewById(R.id.et_register_account);
        mPasswordView = (EditText) findViewById(R.id.et_register_password);
        mNickNameView = (EditText) findViewById(R.id.et_register_nickname);
        mDelAccountView = findViewById(R.id.btn_register_account_del);
        mDelPasswordView = findViewById(R.id.btn_password_show);
        mDelNickNameView = findViewById(R.id.btn_register_nickname_del);
        findViewById(R.id.btn_login_back).setOnClickListener(this);
        findViewById(R.id.btn_register).setOnClickListener(this);
        mDelAccountView.setOnClickListener(this);
        mDelPasswordView.setOnClickListener(this);
        mDelNickNameView.setOnClickListener(this);
        mAccountView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    mDelAccountView.setVisibility(View.VISIBLE);
                } else {
                    mDelAccountView.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mPasswordView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    mDelPasswordView.setVisibility(View.VISIBLE);
                } else {
                    mDelPasswordView.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mNickNameView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    mDelNickNameView.setVisibility(View.VISIBLE);
                } else {
                    mDelNickNameView.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_register:
                if (ThreeWayUtil.getInstance().isPhoneNum(getEditContent(mAccountView))) {
                    ThreeWayUtil.getInstance().showToast(this, getResources().getString(R.string.phone_format_error));
                    getEditFocus(mAccountView);
                } else if(getEditContent(mAccountView).equals(ThreeWayUtil.getInstance().getLoginAccount(this))){
                    ThreeWayUtil.getInstance().showToast(this, getResources().getString(R.string.register_error));
                } else if (getEditContent(mPasswordView).length() < 6 || getEditContent(mPasswordView).length() > 16) {
                    ThreeWayUtil.getInstance().showToast(this, getResources().getString(R.string.tip_password_error));
                    getEditFocus(mPasswordView);
                } else if (getEditContent(mNickNameView).length() < 2 || getEditContent(mNickNameView).length() > 24) {
                    ThreeWayUtil.getInstance().showToast(this, getResources().getString(R.string.tip_nickname_error));
                    getEditFocus(mNickNameView);
                }  else {
                    goCheckCodeDialog(getEditContent(mAccountView),getEditContent(mPasswordView),getEditContent(mNickNameView));
                }
                break;
            case R.id.btn_login_back:
                finish();
                break;
            case R.id.btn_register_account_del:
                setViewEmpty(mAccountView);
                break;
            case R.id.btn_register_nickname_del:
                setViewEmpty(mNickNameView);
                break;
            case R.id.btn_password_show:
                setViewEmpty(mPasswordView);
                break;
        }
    }

    private String getEditContent(EditText editText) {
        return editText.getText().toString();
    }

    private void setViewEmpty(EditText editText) {
        editText.setText("");
    }

    private void getEditFocus(EditText editText) {
        editText.requestFocus();
    }

    public void goCheckCodeDialog(final String phoneNum,final String password, final String userName) {
        CheckCodeDialog checkCodeDialog = CheckCodeDialog.newInstance(phoneNum,password,userName);
        checkCodeDialog.show(getSupportFragmentManager(), "checkCodeDialog");
    }

    public void goLoginActivity() {
        finish();
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(intent);
    }
}
